//
//  SAPSearchDetailsViewController.m
//  CarsHome
//
//  Created by dapeng on 15/12/18.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPSearchDetailsViewController.h"

@interface SAPSearchDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) SAPFindCarOneModel *oneModel;
@end

@implementation SAPSearchDetailsViewController
- (void)dealloc {
    [_tableView release];
    [_oneModel release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableViews];
    _HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _HUD.color = [UIColor blackColor];
    _HUD.dimBackground = YES;
    _HUD.labelText = @"正在加载";
    [SAPNetWorkTool getWithUrl:_url parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
        NSDictionary *dic = [result valueForKey:@"result"];
        self.oneModel = [[SAPFindCarOneModel alloc] initWithDataSource:dic];
        _HUD.hidden = YES;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];

}
- (void)tableViews {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[SAPSearchDetailTableViewCell class] forCellReuseIdentifier:@"SAPSearchDetailTableViewCellIndentifier"];
    [self.view addSubview:self.tableView];
    [_tableView release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAPSearchDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPSearchDetailTableViewCellIndentifier"];
    SAPFindCarTwoModel *twoModel = self.oneModel.listArray[indexPath.row];
    cell.searchText = self.searchText;
    cell.twoModel = twoModel;
    
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.oneModel.listArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SCREEN_SIZE.height / 8;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SAPFindCarTwoModel *twoModel = self.oneModel.listArray[indexPath.row];
    NSString *string = [NSString stringWithFormat:@"http://comm.app.api.autohome.com.cn/static/sharejump.html?p=1&pm=1&go=1&cont=5&ourl=http://m.autohome.com.cn/%@/", twoModel.id];
    SAPSellDetailsViewController *detailVC = [[SAPSellDetailsViewController alloc] init];
    detailVC.url = string;
    [self.navigationController pushViewController:detailVC animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
